﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.ComponentModel;



namespace AlgoLogicMessengerCompetitor
{
    /// <summary>
    /// Interaction logic for Games.xaml
    /// </summary>
    public partial class Games : Page
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
        BackgroundWorker worker = new BackgroundWorker();
        Thread ctThread1;


        public Games()
        {
            InitializeComponent();

            clientSocket.Connect(IP.Address, 8888);
            serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("" + "#");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                getCommand();
            };
            worker.RunWorkerAsync();
        
        }


        private void getCommand()
        {
            while (true)
            {
                try
                {
                    serverStream = clientSocket.GetStream();
                    int bufferSize = clientSocket.ReceiveBufferSize;
                    byte[] inStream = new byte[bufferSize];
                    serverStream.Read(inStream, 0, bufferSize);

                    string str = System.Text.Encoding.ASCII.GetString(inStream, 0, inStream.Length);
                    str = str.Replace("\0", "");
                    string[] array1 = str.Split('|');

                    if (str.StartsWith("@quizStart"))
                    {
                        Thread.Sleep(100);
                        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            NavigationFrame.Content = null;
                            NavigationFrame.NavigationService.RemoveBackEntry();
                            QuestionAnswer Qa = new QuestionAnswer();
                            NavigationFrame.Content = Qa;
                        }
                          )
                          );
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void BtnBack_Click(object sender, RoutedEventArgs e)
        {
            NavigationFrame.Content = null;
            NavigationFrame.NavigationService.RemoveBackEntry();
            NavigationPage Qa = new NavigationPage();
            NavigationFrame.Content = Qa;

            //NavigationService nav = NavigationService.GetNavigationService(this);
            //nav.Navigate(new System.Uri("NavigationPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void Game1_Click(object sender, RoutedEventArgs e)
        {
            NavigationFrame.Content = null;
            NavigationFrame.NavigationService.RemoveBackEntry();
            GameWindow Qa = new GameWindow();
            NavigationFrame.Content = Qa;

            //NavigationService nav = NavigationService.GetNavigationService(this);
            //nav.Navigate(new System.Uri("GameWindow.xaml", UriKind.RelativeOrAbsolute));
        }

        private void Game2_Click(object sender, RoutedEventArgs e)
        {
            NavigationFrame.Content = null;
            NavigationFrame.NavigationService.RemoveBackEntry();
            Reversi Qa = new Reversi();
            NavigationFrame.Content = Qa;

            //NavigationService nav = NavigationService.GetNavigationService(this);
            //nav.Navigate(new System.Uri("Reversi.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}